package Entity;

import java.util.Vector;
import java.util.*;

public abstract class Tarantula {

    public static Vector<Rank> getRankings(int[][] coverageMatrix, int[] resultVector) //Don't really need other 2...they can be computed
    {
        Vector<Rank> rankings = new Vector<Rank>();
        rankings.clear();

        int numTests = coverageMatrix.length;
        int numStmts = coverageMatrix[0].length;

        int numPassed = 0;
        int numFailed = 0;

        //System.out.println(numTests+" "+numStmts);

        for (int i = 0; i < resultVector.length; i++) {
            if (resultVector[i] > 0) {
                numFailed++;
            } else {
                numPassed++;
            }
        }

        for (int i = 0; i < numStmts; i++) {
            int failedAndExecuted = 0;
            int passedAndExecuted = 0;

            for (int j = 0; j < numTests; j++) {
                if (coverageMatrix[j][i] > 0) {
                    if (resultVector[j] == 1) {
                        failedAndExecuted++;
                    } else {
                        passedAndExecuted++;
                    }
                }
            }

            double s1 = ((double) (failedAndExecuted)) / ((double) (numFailed));     //Chance for NaN
            double s2 = ((double) (passedAndExecuted)) / ((double) (numPassed));             //Chance for NaN

            if ((s2 == 0) && (s1 == 0)) {
                s2 = 1;
            }

            double susp = s1 / (s1 + s2);

            if (susp == Double.POSITIVE_INFINITY) {
                susp = 1.0;
            }

            String s = "" + susp;

            if (s.equals("NaN")) {
                susp = 0.0;
            }

            Rank r = new Rank(susp, i);
            rankings.add(r);
        }

        Collections.sort(rankings);

        /*
        for(int i=0;i<rankings.size();i++)
        {
        System.out.println(rankings.elementAt(i).stmt+"\t"+rankings.elementAt(i).susp+"\t"+(i+1));
        }
        System.out.println("----- END TARANTULA-----");
         */

        return ((Vector<Rank>) (rankings.clone()));
    }
}